<!--
// 
// login.vue
// 海石后台管理
// 
// 2024/11/19.
// 
-->
<template>
    <div class="login">
        <div class="bg"></div>
        <div class="bg1"></div>
        <g-flex class="before" column justify-content-start align-items-center>
            <g-flex width="400px" row justify-content-center align-items-center class="logo-name">
                <img src="@/assets/logos/logo_03.jpg" />
                <div class="name">海石软件管理后台</div>
            </g-flex>
            <g-flex v-loading="isSubmit" class="container" row width="unset">
                <div v-if="layoutType === 'pc'" class="image">
                    <img src="@/assets/login-bg/搜索.svg" />
                </div>
                <g-flex class="content" column width="340px">
                    <g-flex row justify-content-center align-items-center class="login-title">
                        <div class="name">登录</div>
                    </g-flex>
                    <g-flex column>
                        <div class="login-input-row">
                            <g-flex class="icon" width="40px" height="40px" row justify-content-center align-items-center>
                                <b-icon name="bi-person" size="22"/>
                            </g-flex>
                            <input v-model="model.userName" class="input" placeholder="账号"/>
                        </div>
                        <div class="login-input-row">
                            <g-flex class="icon" width="40px" height="40px" row justify-content-center align-items-center>
                                <b-icon name="bi-shield-lock"/>
                            </g-flex>
                            <input v-model="model.password" @keydown="onEnter" class="input" placeholder="密码" type="password"/>
                        </div>
                        <g-flex @click="remember=!remember" row class="remember-account">
                            <input type="checkbox" class="checkbox" :checked="remember"/>
                            <span>记住账号</span>
                        </g-flex>
                        <g-flex @click="beforeLogin" row justify-content-center align-items-center class="login-button">立即登录</g-flex>
                    </g-flex>
                </g-flex>
            </g-flex>
        </g-flex>
    </div>
</template>

<script setup>
import {ref, reactive, computed, watch, onMounted} from "vue";
import {useStore} from "vuex";
import {useRouter} from "vue-router";
import LocalDB from "@/utils/LocalDB";
import * as api from "./api.js"
import UserModel from "@/models/UserModel";
import GFlex from "@/components/GFlex/GFlex.vue";

// 路由
const router = useRouter();
// store
const store = useStore();
const layoutType = computed(() => store.getters.layoutType);

// 数据模型
const model = reactive({
    userName: null,
    password: null,
    code: null,
    codeId: null
})
// 记住账号
const remember = ref(false)
// 提交中
const isSubmit = ref(false);

watch(() => remember.value, val => {
    console.log("记住账号: ", val);
});
// 本地存储账号信息
function writeCache() {
    if (remember.value) {
        LocalDB.setItem("loginAccount", model.userName);
    }
}

// 本地读取账号信息
function readCache() {
    const localDBItem = LocalDB.getItem("loginAccount");
    if (!localDBItem.data) return;
    model.userName = localDBItem.data;
}

// 登录前事件 (表单校验)
function beforeLogin() {
    if (!model.userName) return $message.warning("请输入账号");
    if (!model.password) return $message.warning("请输入密码");
    login();
}
// 回车事件
function onEnter(e) {
    if (e.code === "Enter") {
        beforeLogin()
    }
}
// 登录
async function login() {
    isSubmit.value = true;
    const body = {...model}
    const {success, data} = await api.Login(body);
    isSubmit.value = false;
    if (!success) return // refreshImgCode();
    $message.success("登录成功")
    // 先查用户信息, 然后查菜单
    const res = await api.GetCurrentUserInfo();
    const userInfo = new UserModel(res.data || {});
    store.dispatch("userInfo", userInfo);
    // 清空上个用户菜单信息
    store.dispatch("sideMenus", [])
    writeCache();
    let redirect = "/"
    LocalDB.removeItem("redirect")
    window.location.href = redirect
}

onMounted(() => {
    readCache();
})
</script>

<style lang="scss" scoped>

$input-row-background-color: rgba(0, 0, 0, 0);
$input-row-border-color: rgba(0, 0, 0, 0.2);
$input-placeholder-color: #777;
$input-color: #333;
$input-row-border-focus-color: #106ebe;

.login {
    width: 100%;
    height: 100vh;
    user-select: none;
    position: relative;
    background: #fff;
    //background: var(--el-color-primary-light-3);
    .bg {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        //background: url("@/assets/login-bg/bg3.png") no-repeat;
        background: url("@/assets/background.svg") no-repeat;
        background-size: cover;
        z-index: 0;
    }
    .bg1 {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        //background: url("@/assets/login-bg/bg3.png") no-repeat;
        background: url("@/assets/background-text.svg") no-repeat;
        background-size: cover;
        z-index: 1;
        &::after {
            position: absolute;
            content: " ";
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            //background: rgba(0, 0, 0, 0.6);
            //backdrop-filter: blur(4px);
        }
    }
    .before {
        position: absolute;
        top: 0;
        left: 0;
        width: 100%;
        height: 100%;
        z-index: 2;

        .logo-name {
            width: 400px;
            margin-top: 40px;
            img {
                width: 60px;
                height: 60px;
            }
            .name {
                font-weight: bold;
                font-size: 36px;
                color: var(--el-color-primary);
                padding-left: 10px;
                //text-shadow: 0 0 1px rgba(0, 0, 0, 1);
            }
        }
        .container {
            border-radius: 10px;
            overflow: hidden;
            box-sizing: border-box;
            border: 2px solid var(--el-color-primary);
            box-shadow: 0 0 20px 0 rgba(255, 255, 255, 0.3);
            margin-top: 100px;

            .image {
                width: 400px;
                height: 500px;
                background: #fff;
                border-right: 1px solid #f0f0f0;

                img {
                    width: 100%;
                    height: 100%;
                }
            }
            .content {
                background: rgba(255, 255, 255, 1);
                backdrop-filter: blur(20px);
                box-sizing: border-box;
                padding: 40px 30px 20px 30px;
                .login-title {
                    color: var(--el-color-primary);
                    font-size: 30px;
                    font-weight: bold;
                }
                .login-input-row {
                    width: 100%;
                    height: 44px;
                    box-sizing: border-box;
                    margin-top: 40px;
                    position: relative;
                    .icon {
                        position: absolute;
                        left: 2px;
                        top: 2px;
                        bottom: 2px;
                        z-index: 1;
                        width: 40px;
                        height: 30px;
                        box-sizing: border-box;
                        border-radius: 6px 0 0 6px;
                        border-right: 1px solid $input-row-border-color;
                        color: $input-placeholder-color;
                    }
                    .input {
                        text-indent: 50px;
                        box-sizing: border-box;
                        width: 100%;
                        height: 100%;
                        border: 1px solid $input-row-border-color;
                        background: $input-row-background-color;
                        border-radius: 6px;
                        font-size: 14px;
                        color: $input-color;
                        outline: none;
                        &:focus {
                            border: 1px solid $input-row-border-focus-color;
                        }
                        &::placeholder {
                            color: $input-placeholder-color;
                        }
                    }
                }
                .remember-account {
                    margin-top: 40px;
                    color: var(--el-color-primary);
                    .checkbox {
                        width: 16px;
                        height: 16px;
                        accent-color: var(--el-color-primary);
                    }
                }
                .login-button {
                    width: 100%;
                    height: 44px;
                    border-radius: 6px;
                    background: var(--el-color-primary);
                    color: #fff;
                    font-size: 16px;
                    margin-top: 20px;
                    cursor: pointer;
                    &:active {
                        background: var(--el-color-primary-dark-2);
                    }
                }
            }
        }
    }

}
</style>